public with sharing class DataImportToSFController {
   
	public  String msg {private set; get;}
	public Boolean isSuc {private set; get;}
	public String saveJson { set; get;}


    public DataImportToSFController() {
        
    }


    public void saveDz(){
    	msg= '';
    	SavePoint sp  = Database.setSavepoint();
    	try {
    	    
    	    List<Object> saveList =(List<Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
    	    Map<String,Id> userMap = new Map<String,Id>();
    	    for(Object o : saveList){
    	    	Map<String,Object> mo = (Map<String,Object>)o;
    	    	userMap.put(DataUtil.getStringValue(mo.get('XSKZRRNAME')), null);
    	    	userMap.put(DataUtil.getStringValue(mo.get('ZRJLNAME')), null);

    	    }
    	    //检查责任人
    	    List<String> notActives = new List<String>();
    	    List<String> notFinds = new List<String>();

    	    for(User u : [select Id,Name,IsActive from User where Name in:userMap.keySet() ]){
    	    	if(u.IsActive ){
    	    		userMap.put(u.Name, u.Id );
    	    	}else{
    	    		//notActives.add(u.Name );
    	    	}
    	    }

    	    Map<String,AcceptancePerformance__c> aMap = new Map<String,AcceptancePerformance__c>();
    	    Map<String,List< AccountsReceivableStatement__c>> adetailMap = new Map<String,List< AccountsReceivableStatement__c>>();

    	    for(Object o : saveList){
    	    	Map<String,Object> mo = (Map<String,Object>)o;
    	    	 
    	    	 String kpiperiod = DataUtil.getStringValue(mo.get('YEARQUARTER_'));
    	    	 String saler = DataUtil.getStringValue(mo.get('XSKZRRNAME'));
    	    	 String manager = DataUtil.getStringValue(mo.get('ZRJLNAME'));
    	    	 String key = kpiperiod + saler;
    	    	 if(String.isBlank(saler) || String.isBlank(manager)){
    	    	 	continue;
    	    	 }

    	    	 if(!aMap.containsKey(key)){
    	    	 	 AcceptancePerformance__c ap = new  AcceptancePerformance__c();
	    	    	 ap.KpiPeriod__c =kpiperiod;
	    	    	 

	    	    	 ap.OwnerId  = userMap.get(saler);
	    	    	 if(ap.OwnerId == null ){
	    	    	 	continue;
	    	    	 	notFinds.add(saler);

	    	    	 }
	    	    	 ap.ManagerName__c   = userMap.get(manager);
	    	    	 if(ap.ManagerName__c == null ){
	    	    	 	continue;
	    	    	 	notFinds.add(manager);
	    	    	 	
	    	    	 }


	    	    	 
	    	    	 aMap.put( key, ap);
    	    	 }
    	    	

    	    	List< AccountsReceivableStatement__c> details =  adetailMap.containsKey(key) ? adetailMap.get(key) : new List< AccountsReceivableStatement__c>();
    	    	details.add(transTo(mo));
    	    	adetailMap.put(key, details);


    	    }


    	    if(!notActives.isEmpty())msg += String.join(notActives, ',')+' 帐号关闭;';

    	    if(!notFinds.isEmpty())msg += String.join(notFinds, ',')+' 没有找到SF帐号;';

    	    if(String.isNotBlank(msg)) throw new DmlException(msg);



    	    insert aMap.values();
    	    List<AccountsReceivableStatement__c> details = new List<AccountsReceivableStatement__c>();
    	    for (String key  : aMap.keySet()) {
    	    	AcceptancePerformance__c ap = aMap.get(key);
    	    	for(AccountsReceivableStatement__c  ad :adetailMap.get(key) ){
    	    		ad.AcceptancePerformance__c = ap.Id;
    	    		details.add(ad);
    	    	}
    	    }

    	    insert details;
    	    saveJson  ='[]';
    	    isSuc = true;
    	    msg = '保存成功!';

    	}catch (Exception e) {
    		saveJson  ='[]';
    	  	Database.rollback(sp);
    	  	msg = '保存失败:'+e.getMessage();

    	}
    

    }


    private AccountsReceivableStatement__c transTo(Map<String,Object> mo){

    	AccountsReceivableStatement__c ars = new AccountsReceivableStatement__c();
    	// {field:'ACCOUNTCODE_NC',title:'客户编码',width:120},
    	// 		{field:'ACCOUNTNAME',title:'客户名称',width:120},
    	// 		{field:'RECORDTYPE',title:'客户属性',width:120},
    	// 		{field:'TERMNAME',title:'信用期',width:120},
    	ars.Customer__c = DataUtil.getStringValue(mo.get('ACCOUNTNAME'));
    	ars.AccountCode__c =  DataUtil.getStringValue(mo.get('ACCOUNTCODE_NC'));
    	ars.CustomerAttributes__c = DataUtil.getStringValue(mo.get('RECORDTYPE'));
    	ars.CreditPeriod__c	 = DataUtil.getStringValue(mo.get('TERMNAME'));
    	//{field:'UNITSHORTNAME_NC',title:'开票公司',width:120},
    	ars.BillingCompany__c =  DataUtil.getStringValue(mo.get('UNITSHORTNAME_NC'));
    	//{field:'UNITSHORTNAME_GK',title:'归口公司',width:120},
    	ars.CentralizedCompany__c =  DataUtil.getStringValue(mo.get('UNITSHORTNAME_GK'));
    	ars.WhetherNewSalesman__c =   DataUtil.getStringValue(mo.get('SFXJS'));
    	ars.Agent__c =  DataUtil.getStringValue(mo.get('SFDLYWY'));

    	//	{field:'SJDYSZE',title:'上季度应收<br/>总额',width:120},
    			// {field:'BJDYSYE',title:'本季度应收<br/>余额',width:120},
    			// {field:'ZJSJDCQZE',title:'截止上季度<br/>超期总额',width:120},
    	//上季度应收总额
    	ars.LastReceivables__c = DataUtil.getDecimalValue(mo.get('SJDYSZE'));
    	//本季度应收余额
    	ars.CurrentReceivables__c = DataUtil.getDecimalValue(mo.get('BJDYSYE'));
    	//	截止上季度超期总额
    	ars.LastOverdueAmount__c = DataUtil.getDecimalValue(mo.get('ZJSJDCQZE'));
    	//    			{field:'JZBJDCQZE',title:'截止本季度<br/>超期总额',width:120},

    	ars.CurrentOverdueAmount__c = DataUtil.getDecimalValue(mo.get('JZBJDCQZE'));
    	//{field:'MONEYMAIN',title:'本季度回款',width:120},
    	ars.ReagentAccessoriesBackAmount__c = DataUtil.getDecimalValue(mo.get('MONEYMAIN'));
	
    	//		{field:'TAXNMNY',title:'本季度销售<br/>收入',width:120},
    	ars.ReagentAccessoriesSalesRevenue__c = DataUtil.getDecimalValue(mo.get('TAXNMNY'));

    	//		{field:'CQESY',title:'超期1-3个月',width:120},
    	ars.Overdue__c = DataUtil.getDecimalValue(mo.get('CQESY'));
    	//{field:'CQSLY',title:'超期3-6个月',width:120},
    	ars.Over3MonthsOfOverdueDate__c = DataUtil.getDecimalValue(mo.get('CQSLY'));
    	//{field:'CQLYYS',title:'超期6个月以上',width:120},
    	ars.Over6MonthsOfOverdueDate__c = DataUtil.getDecimalValue(mo.get('CQLYYS'));
    	//{field:'BJDCQTZE',title:'本季度超期<br/>调整额',width:120},
    	ars.AdjustmentAmount1__c = DataUtil.getDecimalValue(mo.get('BJDCQTZE'));

		//{field:'BJD_AD_CQ',title:'本季度调整<br/>后超期',width:120},
		ars.OverdueAfterAdjustment__c = DataUtil.getDecimalValue(mo.get('BJD_AD_CQ'));

		//{field:'BJD_AD_PER',title:'当期超期<br/>环比变动',width:120},
    	ars.OverdueChangeRatio__c = DataUtil.getDecimalValue(mo.get('BJD_AD_PER'));
    	//		{field:'BJD_AD_SCORE',title:'超期环<br/>比得分',width:120},
    	ars.Score__c = DataUtil.getDecimalValue(mo.get('BJD_AD_SCORE'));
    	//{field:'BJDZDWCQK',title:'本季度对账<br/>完成情况',width:120},
    	//ars.Reconciliation__c = DataUtil.getStringValue(mo.get('BJDZDWCQK'));
    	//{field:'BJD_AD_DZQK',title:'本季度对账<br/>扣款额',width:120},
    	ars.Deduction1__c = DataUtil.getDecimalValue(mo.get('BJD_AD_DZQK'));
    	//{field:'BJD_AD_CQQK',title:'超期30天/90<br/>天扣款',width:120},
    	ars.Deduction2__c = DataUtil.getDecimalValue(mo.get('BJD_AD_CQQK'));
    	//{field:'BJD_AD_XSQK',title:'无销售无<br/>回款扣款',width:120},
    	ars.Deduction3__c = DataUtil.getDecimalValue(mo.get('BJD_AD_XSQK'));


		//{field:'BJDDZTZE',title:'本季度对账<br/>调整额',width:120},
		ars.AccountDeductionInThisQuarterAdjust__c = DataUtil.getDecimalValue(mo.get('BJDDZTZE'));
		//{field:'CQSJSTZE',title:'超期30天/90天<br/>超期调整额',width:120},
		ars.AdjustmentAmount2__c = DataUtil.getDecimalValue(mo.get('CQSJSTZE'));
		//{field:'WXSWHKGKTZE',title:'无销售无回款<br/>扣款调整',width:120},
		ars.AdjustmentAmount3__c = DataUtil.getDecimalValue(mo.get('WXSWHKGKTZE'));
		//{field:'BJD_ADA_DZQK',title:'本季度调整后<br/>对账扣款',width:120},
		ars.DeductionAfterAdjustment1__c = DataUtil.getDecimalValue(mo.get('BJD_ADA_DZQK'));
		//{field:'BJD_ADA_CQQK',title:'超期30天/90天<br/>调整后超期扣款',width:120},
		ars.DeductionAfterAdjustment2__c = DataUtil.getDecimalValue(mo.get('BJD_ADA_CQQK'));
		//{field:'BJD_ADA_XSQK',title:'调后无销售无<br/>回款扣款',width:120},
		ars.DeductionAfterAdjustment3__c = DataUtil.getDecimalValue(mo.get('BJD_ADA_XSQK'));
		//{field:'SJDGK',title:'上季度扣款',width:120},
		ars.Deduction4__c = DataUtil.getDecimalValue(mo.get('SJDGK'));
		return ars;
    }
}